package com.common.util;

import com.curry.android.util.StringUtil;

/**
 * 临时的，准备移动到BaseLibrary中去
 * <p>
 * Created by 吴格非 on 2016-10-26.
 * <p>
 *
 * @author 吴格非
 * @since v4.1.0
 */

public class MoneyUtil2 {

    /** 橙汁去〇用 */
    public static String trimZero(String source) {
        if (source.indexOf(".") > 0) {
            source = source.replaceAll("0+?$", "");
            source = source.replaceAll("[.]$", "");
        }
        return source;
    }

    public static String handleMoney(String money, boolean isAllowedNegativeNum) {
        String moneyResult = "0.00";
        if (!StringUtil.isBlank(money)) {
            if (money.contains("-") && !isAllowedNegativeNum) {
                money = "0";
            }

            if (money.contains(".")) {
                int indexDot = money.indexOf(".");
                if (indexDot == 0) {
                    moneyResult = "0.00";
                } else if (indexDot + 1 == money.length()) {
                    moneyResult = money + "00";
                } else if (money.length() - (indexDot + 1) == 1) {
                    moneyResult = money + "0";
                } else if (money.length() - (indexDot + 1) > 2) {
                    moneyResult = money.substring(0, indexDot + 1 + 2);
                } else {
                    moneyResult = money;
                }
            } else {
                moneyResult = money + ".00";
            }
        }

        return moneyResult;
    }

}
